-- INCLUDES
VFS.Include("LuaRules/Gadgets/Includes/utilities.lua")
VFS.Include("LuaRules/Gadgets/Includes/messagetypes.lua")

-- CONSTANTS
local GAIA_TEAM_ID = Spring.GetGaiaTeamID()
local MAXUNITLIMIT = 20;
local UNITCOST = "unitcost"

-- SYNCED CONTROL
Echo              = Spring.Echo
MessageDispatcher = Spring.SendLuaUIMsg

-- MEMBERS
local teamLimits = {}
local gameError = false

-- SCRIPT FUNCTIONS
function getUnitCost(unitDefID)
	local params = UnitDefs[unitDefID]["customParams"]
	if(params ~= nil) then
		return params[UNITCOST];
	else
		gameError = true
		Echo("unitdef " .. unitDefID .. " has no " .. UNITCOST)
		return nil
	end
end

-- GADGET CODE
function gadget:GetInfo()
	return {
		name      = "Limit manager",
		desc      = "Keeps track of the teams unit costs and makes sure you don't exceed the max unit limit",
		author    = "Sunspot",
		date      = "2011-06-14",
		license   = "GNU GPL v2",
		layer     = -2,
		enabled   = false
	}
end

if (gadgetHandler:IsSyncedCode()) then
	
	function gadget:GamePreload()
	end
	
	function gadget:GameStart()
		-- initialise unit limit tables for each team ingame
		local teamLimitIndex = 1
		local teamList = Spring.GetTeamList()
		for i=1, #teamList do
			teamLimits[teamLimitIndex] = 0
			teamLimitIndex = teamLimitIndex + 1
		end
	end
	
	function gadget:UnitCreated(unitID, unitDefID, teamID, builderID)
		-- add to unit limit for teamid (remember + 1)
		local cost = getUnitCost(unitDefID)
		if(cost) then
			teamLimits[teamID+1] = teamLimits[teamID+1] + cost
			MessageDispatcher(UNITLIMITUPDATE .. "-" .. teamID .. "-" .. teamLimits[teamID+1] .. "--") 
			if DEBUG then Echo("teamID " .. teamID .. " new limit " .. teamLimits[teamID+1]) end
		end
	end

	function gadget:UnitFinished(unitID, unitDefID, teamID)
		-- no action needed
	end

	function gadget:UnitFromFactory(unitID, unitDefID, unitTeam, factID, factDefID, userOrders)
		-- no action needed
	end

	function gadget:UnitDestroyed(unitID, unitDefID, teamID, attackerID)
		-- substract from unit limit for teamid (remember + 1)
		local cost = getUnitCost(unitDefID)
		if(cost) then
			teamLimits[teamID+1] = teamLimits[teamID+1] - cost
			MessageDispatcher(UNITLIMITUPDATE .. "-" .. teamID .. "-" .. teamLimits[teamID+1] .. "--")
			if DEBUG then Echo("teamID " .. teamID .. " new limit " .. teamLimits[teamID+1]) end		
		end
	end

	function gadget:UnitTaken(unitID, unitDefID, unitTeam, newTeam)
		-- substract from unitTeam (+1) and add to newTeam (+1)
		local cost = getUnitCost(unitDefID)
		if(cost) then
			teamLimits[unitTeam+1] = teamLimits[unitTeam+1] - cost
			MessageDispatcher(UNITLIMITUPDATE .. "-" .. unitTeam .. "-" .. teamLimits[unitTeam+1]) 				
			if DEBUG then Echo("teamID " .. unitTeam .. " new limit " .. teamLimits[unitTeam+1]) end		
		end
	end

	function gadget:UnitGiven(unitID, unitDefID, unitTeam, oldTeam)
		-- subtract from oldTeam (+1) and add to unitTeam (+1)
			local cost = getUnitCost(unitDefID)
			if(cost) then
				teamLimits[unitTeam+1] = teamLimits[unitTeam+1] + cost
				MessageDispatcher(UNITLIMITUPDATE .. "-" .. unitTeam .. "-" .. teamLimits[unitTeam+1]) 				
				if DEBUG then Echo("teamID " .. unitTeam .. " new limit " .. teamLimits[unitTeam+1]) end		
			end

	end
	
	function gadget:AllowUnitCreation(unitDefID, builderID, builderTeam, x, y, z)
		-- if no gameError is detected go further
		if not gameError then
			-- check if the new costtotal won't exceed MAXUNITLIMIT
			local cost = getUnitCost(unitDefID)
			local newTotal = teamLimits[builderTeam+1] + cost
			if(newTotal <= MAXUNITLIMIT) then
				return true
			else
				Echo("Unit limit reached")
				return false
			end
		else
			Echo("game error detected building disabled to disable gameplay")
			return false
		end		
	end
else
-- UNSYNCED
end